import BaseView from "../baseView.js";

export default class AddStudent extends BaseView{

    render(){
       $("#showContent").html(`
        <div>
        <div style="margin: 30px 10px 0 20px ">
        <span class="layui-breadcrumb" style="visibility: visible;">
            <a>学生管理系统/</a>/
            <a>学生资料管理/</a>/
            <a>添加学生</a>
         </span>
         <hr/>
        </div>
        <div>
        <form class="layui-form">
        <div class="layui-form-item">
        <label class="layui-form-label">姓名:</label>
          <div class="layui-input-block">
            <input type="text" id="name" required   placeholder="请输入姓名"  class="layui-input">
          </div>
        </div>
        <div class="layui-form-item">
        <label class="layui-form-label">年龄:</label>
          <div class="layui-input-block">
            <input type="text" id="age" required   placeholder="请输入年龄"  class="layui-input">
          </div>
        </div>
        <div class="layui-form-item">
        <label class="layui-form-label">家庭住址:</label>
          <div class="layui-input-block">
            <input type="text" id="address" required   placeholder="请输入家庭住址"  class="layui-input">
          </div>
        </div>
        <div class="layui-form-item">
        <label class="layui-form-label">考试分数:</label>
          <div class="layui-input-block">
            <input type="text" id="score" required   placeholder="请输入考试分数"  class="layui-input">
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">性别:</label>
          <div class="layui-input-block">
            <input type="radio" name="gender" value="男" title="男" checked> 
            <input type="radio" name="gender" value="女" title="女" >
          </div>
        </div>
        <div class="layui-form-item">
          <div class="layui-input-block">
            <button type='button' id='addstudentBtn' class="layui-btn" lay-submit lay-filter="formDemo">添加</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
          </div>
        </div>
      </form>
        </div>
        
        </div>
       `)
    }


    handle(){
       $("#addLayuijs").html(`
       <script>
        layui.use('form', function(){
        var form = layui.form;
         //重新渲染表单
         form.render();
        });
        </script>
         `)

         //绑定添加事假
         $("#addstudentBtn").click(()=>{
             $.ajax({
                 type:"post",
                 url:"/api/student/addstudent",
                 data:{
                  "name":$("#name").val(),
                 "age":$("#age").val(),
                 "gender":$("input[type='radio']:checked").val(),
                  "address":$("#address").val(),
                  "score":$("#score").val()
                 },
                 dataType:"json",
                 success:(data)=>{
                    //console.log(data)
                    if(data.code==1){
                       alert(data.msg);
                       window.location.hash="#/main/stundentlist";
                    }else{
                        alert(data.msg);
                    }
                 }
             })
         })
        }
}